<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>新增管理员</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="__ADMIN__/lib/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="__ADMIN__/css/font.css">
    <link rel="stylesheet" href="__ADMIN__/css/xadmin.css">
    <link rel="stylesheet" href="__ADMIN__/css/div.css">
    <script type="text/javascript" src="__ADMIN__/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="text">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>ID</label>
                    <div class="layui-input-inline">
                        <input type="text" id="id" name="id" required="" lay-verify="required|number|idcheck"
                               autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为用户唯一标识
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" required="" lay-verify="required|expertname"
                               autocomplete="off"
                               class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>密码</label>
                    <div class="layui-input-inline">
                        <input type="password" id="pwd" name="pwd" required="" lay-verify="required|pass"
                               autocomplete="off" class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>是否启用</label>
                    <div class="layui-input-inline">
                        <select id="use" name="use" lay-verify="required" autocomplete="off" class="layui-input">
                            <option value="-1" selected>请选择</option>
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>layui.use(['form', 'layer', 'jquery'], function () {
    $ = layui.jquery;
    var form = layui.form,
        layer = layui.layer;
    //自定义验证规则
    form.verify({
        idcheck: function (value) {
            value = $('#id').val();
            //console.log(value);
            if (!(((value.length) > 0) && ((value.length <= 10)))) {
                return '请输入正确的ID!';
            }
            if (verifyStudentIsExist()) {
                return 'ID已存在，请修改ID！';
            }
        },
        expertname: [/^[\u4e00-\u9fa5]{2,4}$|^[a-zA-Z]{1,30}$/, '姓名只能为2至4个汉字或1至30个英文单词'],
        pass: [/^[a-zA-Z]\w{5,17}$/, '以字母开头，长度在6~18之间，只能包含字母、数字和下划线'],
    });

    function verifyStudentIsExist() {
        var expertid = $('#id').val();
        var resCode;
        $.ajax({
            url: "../Admin/adminidcheck",
            type: 'POST',
            dataType: "json",
            data: {"id": expertid},
            async: false,//必须是同步提交的
            success: function (res) {
                // console.log(res.data);
                if (res.data == 1) {
                    //  console.log(res.data);
                    resCode = 1;//数据库中有该用户数据！
                } else if (res.data == 0) {
                    //console.log(res.data);
                    resCode = 0;//数据库中没有该用户数据！
                }
            },
        });
        if (resCode == 1) {
            return true;
        } else if (resCode == 0) {
            return false;
        }
    }

    //监听提交
    form.on('submit(add)', function (data) {
        var id = data.field.id;
        var name = data.field.name;
        var pwd = data.field.pwd;
        var status = data.field.use;
        $.ajax({
            url: "../Admin/admininsert",
            type: "post",
            dataType: "json",
            data: {
                "id": id, "name": name, "pwd": pwd, "status": status
            },
            async: false,//必须是同步提交的
            success: function (t) {
                console.log(t);
            },
        });
        layer.alert("增加成功", {
                icon: 6
            },
            function () {
                //关闭当前frame
                xadmin.close();
                // 可以对父窗口进行刷新
                xadmin.father_reload();
            });
        return false;
    });
});
</script>
</body>
</html>